﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Data;

namespace QAAdmin.BusinessObject
{
    class QAController
    {
        private static string username;
        public static string Username
        {
            get { return username; }
            set { username = value; }
        }

        private static string jenisSPT;
        public static string JenisSPT
        {
            get { return jenisSPT; }
            set { jenisSPT = value; }
        }

        private static TipeUSER userrole;
        public static TipeUSER UserRole
        {
            get { return userrole; }
            set { userrole = value; }
        }

        /*private static string docid;
        public static string DocId
        {
            get { return docid; }
            set { docid = value; }
        }*/

        private static TipeSPT tipeSPT;
        public static TipeSPT TipeSPT
        {
            get { return tipeSPT; }
            set { tipeSPT = value; }
        }

        private static QAJob job;
        public static QAJob Job
        {
            get { return job; }
            set { job = value; }
        }

        internal static IList<QAJob> GetBalancedJob()
        {
            return DAO.GetJobByType(TipeSPT.Balance.ToString().ToLower());
        }

        internal static IList<QAJob> GetUnbalancedJob()
        {
            return DAO.GetJobByType(TipeSPT.Unbalance.ToString().ToLower());
        }

        internal static IList<Petugas> GetBalancedPetugas()
        {
            return DAO.GetPetugasByTipeAndLogin(TipeSPT.Balance, 1);
        }

        internal static IList<Petugas> GetUnbalancedPetugas()
        {
            return DAO.GetPetugasByTipeAndLogin(TipeSPT.Unbalance, 1);
        }       

        internal static IList<Petugas> GetOfflinePetugasBalance()
        {
            return DAO.GetPetugasByTipeAndLogin(TipeSPT.Balance, 0);
        }

        internal static IList<Petugas> GetOfflinePetugasUnBalance()
        {
            return DAO.GetPetugasByTipeAndLogin(TipeSPT.Unbalance, 0);
        }

        internal static IList<QAJob> GetNewUnbalancedJob(int jumlahSPT)
        {
            return DAO.GetNewUnbalancedJob(jumlahSPT);
        }

        internal static IList<QAJob> GetNewBalancedJob(double faktorPersentase, int jumlahSPT)
        {
            return DAO.GetNewBalancedJob(faktorPersentase, jumlahSPT);
        }

        internal static IList<QAJob> GetJobByPetugas(string namapetugas)
        {
            return DAO.GetJobByPetugas(namapetugas);
        }

        internal static IList<QAJob> GetJobByPetugasAndStatus(string namapetugas, string status)
        {
            return DAO.GetJobByPetugasAndStatusAndType(namapetugas, status, tipeSPT.ToString().ToLower());
        }

      /*  internal static QAJob GetJobByDocId(string docId)
        {
            return DAO.GetJobByDocId(docId);
        }*/

        internal static QAJob GetJobByDocIdAndBarcode(string docid, string barcode)
        {
            return DAO.GetJobByDocIdAndBarcode(docid, barcode);
        }

        internal static IList<QAReport> GetListJobReport(string jobId)
        {
            return DAO.GetListJobReport(jobId);
        }

        internal static IList<QAReportPenugasan> GetListJobReportPenugasan()
        {
            return DAO.GetListJobReportPenugasan();
        }

        internal static void CreateNewJobs(List<QAJob> lstJob)
        {
            DAO.CreateNewJobs(lstJob);
        }

        internal static void DeleteJob(QAJob job)
        {
            DAO.DeleteJob(new List<QAJob> { job });
        }

        internal static int DeleteJob(List<QAJob> jobs)
        {
            return DAO.DeleteJob(jobs);
        }

        internal static bool Login(string username, string password)
        {
            if (AD.ValidateUserWithAD(username, password))
            {
                if (DAO.IsFirstTime())
                {
                    userrole = TipeUSER.Administrator;
                    return true;
                }
                else if (DAO.IsValidAdminLogin(username))
                {
                    userrole = TipeUSER.Administrator;
                    return true;
                }
                else if (DAO.IsValidSupervisor(username))
                {
                    userrole = TipeUSER.Supervisor;
                    tipeSPT = DAO.GetTipeSPTFromUser(username);
                    return true;
                }
                // only admin and supervisor
                else
                {
                    return false;
                }
            }
            else return false;
        }

        internal static IList<Petugas> GetAllPetugas()
        {
            return DAO.GetAllPetugas();
        }

        internal static Petugas GetPetugasByName(string username)
        {
            return DAO.GetPetugasByName(username);
        }

        internal static bool IsUsernameExist(string username)
        {
            return DAO.IsUsernameExist(username);
        }

        internal static void BuatPetugas(Petugas petugas)
        {
            DAO.BuatPetugas(petugas);
        }

        internal static void UpdateQAJob(string log, string statusAkhir)
        {
            log = DateTime.Now.ToString("dd MMM yyyy HH:mm:ss ") + log + " oleh " + username + Environment.NewLine;
            string originalLog = job.Catatan;
            DAO.UpdateQAJob(log, originalLog, statusAkhir, job.DocId, job.Barcode);
        }

        internal static void UbahPetugas(Petugas petugas)
        {
            DAO.UbahPetugas(petugas);
        }

        internal static void UbahStatusLoginPetugas(int islogin)
        {
            DAO.UbahStatusLoginPetugas(username, islogin);
        }

        internal static void HapusPetugas(string username)
        {
            DAO.HapusPetugas(username);
        }

        internal static bool DoesUserHaveQAJob(string username)
        {
            return DAO.DoesUserHaveQAJob(username);
        }

        internal static bool IsSupervisor(string username)
        {
            return DAO.IsValidSupervisor(username);
        }

        internal static bool IsAdmin(string username)
        {
            return DAO.IsValidAdminLogin(username);
        }

        internal static string[] GetJenisSPT()
        {
            string[] jenisSPT;
            try
            {
                jenisSPT = ConfigurationManager.AppSettings["JenisSPT"].Split(',');                
            }
            catch (Exception)
            {
                throw new Exception("JenisSPT belum dibuat di .config");
            }
            return jenisSPT;
        }

        internal static DataTable GetSPTWithField(string tableSource, string[] fieldName)
        {
            try
            {
                using (System.Data.OracleClient.OracleConnection con = new System.Data.OracleClient.OracleConnection(DAO.Decrypt(ConfigurationManager.ConnectionStrings["Datacap"].ConnectionString)))
                {
                    return DAO.GetTableWithFieldByField(con, tableSource, fieldName, job.DocId, job.Barcode);
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        internal static DataTable GetSPTWithField(string tableSource, string fieldName)
        {
            try
            {
                using (System.Data.OracleClient.OracleConnection con = new System.Data.OracleClient.OracleConnection(DAO.Decrypt(ConfigurationManager.ConnectionStrings["Datacap"].ConnectionString)))
                {
                    return DAO.GetTableWithFieldByField(con, tableSource, new string[] { fieldName }, job.DocId, job.Barcode);
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        internal static DataTable GetQAChangeLog(string tableSource, string fieldName)
        {
            try
            {
                using (System.Data.OracleClient.OracleConnection con = new System.Data.OracleClient.OracleConnection(DAO.Decrypt(ConfigurationManager.ConnectionStrings["QA"].ConnectionString)))
                {
                    return DAO.GetTableWithFieldByField(con, tableSource, new string[] { fieldName }, job.DocId, job.Barcode);
                }
            }
            catch (Exception)
            {
                return null;
            }
        }

        internal static DataTable GetQAChangeLog(string tableSource, string[] fieldName)
        {
            try
            {
                using (System.Data.OracleClient.OracleConnection con = new System.Data.OracleClient.OracleConnection(DAO.Decrypt(ConfigurationManager.ConnectionStrings["QA"].ConnectionString)))
                {
                    return DAO.GetTableWithFieldByField(con, tableSource, fieldName, job.DocId, job.Barcode);
                }
            }
            catch (Exception)
            {
                return null;
            }
        }
    }
}
